package javabasic.slipch.calendarseeker;

import javabasic.slipch.calendarseeker.Date;

public final class CalendarSeeker {
	
	static public final String getDayOfWeek(final Date date) {
		return dayWeekNumberToName( getDayOfWeekNumber(date) );
	}
	
	static private int getDayOfWeekNumber(final Date date) {
		int cmp = BASE_DATE.compareTo(date);
		if (cmp == 0) {
			return BASE_DAY_OF_WEEK;
		}
		if (cmp < 0) {
			return ( BASE_DAY_OF_WEEK + Date.dayDistance(BASE_DATE, date) - 1 ) % DAYS_IN_A_WEEK  + 1;
		} else {
			return ( BASE_DAY_OF_WEEK + DAYS_IN_A_WEEK - ( Date.dayDistance(date, BASE_DATE) % DAYS_IN_A_WEEK ) - 1) % DAYS_IN_A_WEEK + 1;
		}
	}
	
	static private final String dayWeekNumberToName(int number) {
		switch (number) {
			case 1:	return "Monday";
			case 2: return "Tuesday";
			case 3: return "Wednesday";
			case 4: return "Thursday";
			case 5: return "Friday";
			case 6: return "Saturday";
			case 7: return "Sunday";		
			default: throw new IllegalArgumentException(String.valueOf(number));
		}
	}
	
	private static final int BASE_DAY_OF_WEEK = 2;
	private static final Date BASE_DATE = new Date(2013, 4, 9);
	private static final int DAYS_IN_A_WEEK = 7;
}
